import { app, BrowserWindow, session, nativeTheme } from 'electron'
import { is, optimizer } from '@electron-toolkit/utils'
import registerIpcEvents from './ipc'
import * as mainWindow from './windows/mainWindow'
import store from './store'
const { spawn } = require('child_process');

// 关闭安全警告
process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = true

function initWindow() {
    nativeTheme.themeSource = store.getSetting('theme')
    mainWindow.createWindow()
}

console.log(app.getPath('userData'))

app.whenReady().then(() => {
    initWindow()
    registerIpcEvents()

    // 启动python程序
    startPythonServer()

    app.on('activate', () => {
        if (BrowserWindow.getAllWindows().length === 0) initWindow()
    })

    /* 启用vue.js devtools插件 */
    if (is.dev && import.meta.env['MAIN_VITE_VUE_WEBDEVTOOL_PATH']) {
        session.defaultSession.loadExtension(import.meta.env['MAIN_VITE_VUE_WEBDEVTOOL_PATH'])
    }
})

app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') app.quit()
})

app.on('browser-window-created', (_, window) => {
    window.webContents.openDevTools()
    optimizer.watchWindowShortcuts(window)
})

 // 在这里启动Python子进程
 function startPythonServer() {
    const pythonExePath = `${__dirname}/resources/main.exe`; // 根据实际情况调整路径
    console.log(pythonExePath)
    const pythonProcess = spawn(pythonExePath);

    pythonProcess.stdout.on('data', (data) => {
      console.log(`Python Server Output: ${data}`);
    });

    pythonProcess.stderr.on('data', (data) => {
      console.error(`Python Server Error: ${data}`);
    });

    pythonProcess.on('close', (code) => {
      console.log(`Python Server exited with code ${code}`);
    });
  }
